jeui.use(["jquery", "echarts"], function() {
    data_getAll(function (data) {
        var alldata = datatool(data);
        inityearchart(alldata.yeardata);
        initSupplier(alldata.rankdata);
        initStack(alldata.weektotal);
        initCoordinate(alldata.weekbhv)
    });
});

// console.log(getMonday());

function datatool(ygdata) {
    var datas = {
        weektotal : [],
        weekbhv : [],
        rankdata : {y:0,l:0,z:0,c:0},
        yeardata : {
            total : [],
            base : [],
            progress : [],
            xAxis : []
        }
    };

    var every = ygdata.mydata.every;
    var xws = ['不再犯规','编程词汇', '个人项目','学习进步','认真工作','锻炼身体','丑陋的字'];
    var daynum = 0;
    var xwobj = {};
    var btobj = {};
    var prevtime = null;
    for (var x = 0; x < xws.length; x++) {
        xwobj[xws[x]] = [0, 0, 0, 0, 0, 0, 0];
    }
    for (oneday in every) {
        if(oneday != "details"){
            var obj = every[oneday];
            if (btobj[obj.behavior]) {
                btobj[obj.behavior] += parseInt(obj.grade);
            }else{
                btobj[obj.behavior] = parseInt(obj.grade);
            }
            if(obj.time > getMonday()){
                if (prevtime && prevtime !== obj.time) { //检查时间改变
                    daynum++;
                }
                xwobj[obj.behavior][daynum] = parseInt(obj.grade);
                prevtime = obj.time;
            }
        }
    }
    for(v in  btobj) {
        $("cite:contains('"+v+"')").prev().text(btobj[v]);
    }

    for (var x = 0; x < xws.length; x++) {
        datas.weekbhv.push({
            name: xws[x],
            type: 'bar',
            stack: '行为',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: xwobj[xws[x]]
        });
    }

    var total = ygdata.mydata.total;

    var daynumber = 0;
    for (key in total) {
        if(key != "details"){
            var obj = total[key];
            datas.yeardata.xAxis.push("第" + (++daynumber) + "天");
            datas.yeardata.total.push(obj.grade);
            if (obj.grade - 30 > -1) {
                datas.yeardata.base.push(30);
                datas.yeardata.progress.push(obj.grade - 30);
            } else {
                datas.yeardata.base.push(0);
                datas.yeardata.progress.push(obj.grade);
            }

            if (obj.grade - 85 > -1) {
                datas.rankdata.y += 1;
            } else if (obj.grade - 70 > -1) {
                datas.rankdata.l += 1;
            } else if (obj.grade - 50 > -1) {
                datas.rankdata.z += 1;
            } else {
                datas.rankdata.c += 1;
            }
            if(obj.time > getMonday()){

                var strcolor = "";
                var endcolor = "";
                var num = 0;
                if (obj.rank == "优") {
                    num = parseInt(obj.grade);
                    strcolor = "#8cef7a";
                    endcolor = "#3ce804";
                } else if (obj.rank == "良") {
                    num = parseInt(obj.grade);
                    strcolor = "#81e3ee";
                    endcolor = "#19b7cf";
                } else if (obj.rank == "中") {
                    num = parseInt(obj.grade);
                    strcolor = "#e6cc6d";
                    endcolor = "#ffb100";
                } else {
                    num = parseInt(obj.grade);
                    strcolor = "#fb767b";
                    endcolor = "#cc2e48";
                }
                datas.weektotal.push({
                    "value": num,
                    "itemStyle": {
                        "normal": {
                            "color": new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: strcolor }, { offset: 1, color: endcolor }])
                        }
                    }
                });
            }
        }

    }
    $("cite:contains('犯规次数')").prev().text(parseInt(datas.yeardata.xAxis.length - btobj["不再犯规"]/30));
    return datas;
}

function check(arry) {
    var len = 7 - arry.length;
    for (var i = 0; i < len; i++) {
        arry.push({
            "value": 1,
            "itemStyle": {
                "normal": {
                    "color": new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{ offset: 0, color: "#c8c9ca" }, { offset: 1, color: "#535454" }])
                }
            }
        });
    }
}

function initCoordinate(weekbhv) {
    var weekbhvChart = echarts.init(document.getElementById('coordinate'));
    console.log(weekbhv);
    var weekbhvOption = {
        color: [ "#c23531", "#F37B1D", "#429742",  "#3d9970", "#8E488E", "#0074d9", "#85144b"],
        tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        xAxis: {
            type: 'value'
        },
        yAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        series: weekbhv
    };
    weekbhvChart.setOption(weekbhvOption);
}

function initStack(weektotal) {
    check(weektotal);
    var links = weektotal.map(function(item, i) {
        return {
            source: i,
            target: i + 1
        };
    });
    var weektotalChart = echarts.init(document.getElementById('stack'));
    var weekTotalOption = {
        tooltip: {},
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            type: 'graph',
            layout: 'none',
            coordinateSystem: 'cartesian2d',
            symbolSize: 40,
            label: {
                normal: {
                    show: true
                }
            },
            edgeSymbol: ['circle', 'arrow'],
            edgeSymbolSize: [4, 10],
            data: weektotal,
            links: links,
            lineStyle: {
                normal: {
                    color: '#2f4554'
                }
            }
        }]
    };
    weektotalChart.setOption(weekTotalOption);
}

function initSupplier(data) {
    var rankChart = echarts.init(document.getElementById('supplier'));
    var rankOption = {
        color: ["#fb767b", "#e6cc6d", "#81e3ee", "#8cef7a"],
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['差', '中', '良', '优']
        },
        series: [{
            name: '等级统计',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: [
                { value: data.c, name: '差' },
                { value: data.z, name: '中' },
                { value: data.l, name: '良' },
                { value: data.y, name: '优' }
            ],
            itemStyle: {
                emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }]
    };
    rankChart.setOption(rankOption);
}

function inityearchart(data) {
    var yearchart = echarts.init(document.getElementById('touchscreen'));
    var yearoption = {
        "tooltip": {
            "trigger": "axis",
            "axisPointer": {
                "type": "shadow",
                textStyle: {
                    color: "#fff"
                }

            },
        },
        "grid": {
            "borderWidth": 0,
            "top": 110,
            "bottom": 95,
            textStyle: {
                color: "#fff"
            }
        },
        "legend": {
            x: '4%',
            top: '11%',
            textStyle: {
                color: '#90979c',
            },
            "data": ['进步', '原则']
        },
        "calculable": true,
        "xAxis": [{
            "type": "category",
            "axisLine": {
                lineStyle: {
                    color: '#90979c'
                }
            },
            "splitLine": {
                "show": false
            },
            "axisTick": {
                "show": false
            },
            "splitArea": {
                "show": false
            },
            "axisLabel": {
                "interval": 0,
                "rotate": 45
            },
            "data": data.xAxis,
        }],
        "yAxis": [{
            "type": "value",
            "splitLine": {
                "show": false
            },
            "axisLine": {
                lineStyle: {
                    color: '#90979c'
                }
            },
            "axisTick": {
                "show": false
            },
            "axisLabel": {
                "interval": 0,

            },
            "splitArea": {
                "show": false
            },

        }],
        "dataZoom": [{
            "show": true,
            "height": 30,
            "xAxisIndex": [
                0
            ],
            bottom: 30,
            "start": 90,
            "end": 100,
            handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
            handleSize: '110%',
            handleStyle: {
                color: "#d3dee5",

            },
            textStyle: {
                color: "#fff"
            },
            borderColor: "#90979c"


        }, {
            "type": "inside",
            "show": true,
            "height": 15,
            "start": 1,
            "end": 35
        }],
        "series": [{
            "name": "进步",
            "type": "bar",
            "stack": "评分",
            "barMaxWidth": 35,
            "barGap": "10%",
            "itemStyle": {
                "normal": {
                    "color": "rgba(255,144,128,1)",
                    "label": {
                        "show": true,
                        "textStyle": {
                            "color": "#fff"
                        },
                        "position": "insideTop",
                        formatter: function(p) {
                            return p.value > 0 ? (p.value) : '';
                        }
                    }
                }
            },
            "data": data.progress
        },

            {
                "name": "原则",
                "type": "bar",
                "stack": "评分",
                "itemStyle": {
                    "normal": {
                        "color": "rgba(0,191,183,1)",
                        "barBorderRadius": 0,
                        "label": {
                            "show": true,
                            "position": "top",
                            formatter: function(p) {
                                return p.value > 0 ? (p.value) : '';
                            }
                        }
                    }
                },
                "data": data.base
            }, {
                "name": "总分",
                "type": "line",
                "stack": "评分",
                symbolSize: 10,
                symbol: 'circle',
                "itemStyle": {
                    "normal": {
                        "color": "rgba(252,230,48,1)",
                        "barBorderRadius": 0,
                        "label": {
                            "show": true,
                            "position": "top",
                            formatter: function(p) {
                                return p.value > 0 ? (p.value) : '';
                            }
                        }
                    }
                },
                "data": data.total,
                "markLine": {
                    data: [{
                        type: 'average',
                        name: '平均值',
                        lineStyle: {
                            normal: {
                                color: {
                                    type: 'linear',
                                    x: 0,
                                    y: 0,
                                    x2: 0,
                                    y2: 1,
                                    colorStops: [{
                                        offset: 0,
                                        color: '#fb767b' // 0% 处的颜色
                                    }, {
                                        offset: 1,
                                        color: '#fb767b' // 100% 处的颜色
                                    }],
                                    globalCoord: false // 缺省为 false
                                }
                            }
                        }
                    },
                        {
                            name: '平均值为优',
                            yAxis: 170,
                            lineStyle: {
                                normal: {
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [{
                                            offset: 0,
                                            color: '#8cef7a' // 0% 处的颜色
                                        }, {
                                            offset: 1,
                                            color: '#8cef7a' // 100% 处的颜色
                                        }],
                                        globalCoord: false // 缺省为 false
                                    }
                                }
                            }
                        },
                        {
                            name: '平均值为良',
                            yAxis: 140,
                            lineStyle: {
                                normal: {
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [{
                                            offset: 0,
                                            color: '#81e3ee' // 0% 处的颜色
                                        }, {
                                            offset: 1,
                                            color: '#81e3ee' // 100% 处的颜色
                                        }],
                                        globalCoord: false // 缺省为 false
                                    }
                                }
                            }
                        },
                        {
                            name: '平均值为中',
                            yAxis: 100,
                            lineStyle: {
                                normal: {
                                    color: {
                                        type: 'linear',
                                        x: 0,
                                        y: 0,
                                        x2: 0,
                                        y2: 1,
                                        colorStops: [{
                                            offset: 0,
                                            color: '#e6cc6d' // 0% 处的颜色
                                        }, {
                                            offset: 1,
                                            color: '#e6cc6d' // 100% 处的颜色
                                        }],
                                        globalCoord: false // 缺省为 false
                                    }
                                }
                            }
                        }
                    ]
                }
            },
        ]
    }
    yearchart.setOption(yearoption);
}

function getMonday() {
    var timestring = "",d = new Date();
    if(d.getDay() !== 0){
        d.setDate(d.getDate() -  d.getDay());
    }else {
        d.setDate(d.getDate() -  7);
    }
    var M = d.getMonth()+1 > 9 ? d.getMonth()+1 : "0"+(d.getMonth()+1);
    var D = d.getDate() > 9 ? d.getDate() : "0"+d.getDate();
    timestring = d.getFullYear() +"-"+ M +"-"+ D;
    return timestring;
}